﻿using Omnia.Infrastructure;
using Omnia.Enums;
using System.Data.Linq.Mapping;
using System;

namespace Omnia.DataModels
{
    [Table(Name = "Cards")]
    public class CardInfo : NotificationObject
    {
        #region Fields
        private string guid = System.Guid.NewGuid().ToString();
        private string cardNumber;
        private string issuingBank;
        private string cardMaster;
        private CardKindValue kind = CardKindValue.Debit;
        private string alias;
        private double creditLine = 0;
        private int monthlyRepaymentDay = 1;

        #endregion

        #region Properties
        [Column(IsPrimaryKey=true)]
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        [Column]
        public string CardNumber
        {
            get { return cardNumber; }
            set
            {
                cardNumber = value;
                RaisePropertyChanged(() => CardNumber);
            }
        }

        [Column]
        public string IssuingBank
        {
            get { return issuingBank; }
            set
            {
                issuingBank = value;
                RaisePropertyChanged(() => IssuingBank);
            }
        }

        [Column]
        public string CardMaster
        {
            get { return cardMaster; }
            set
            {
                cardMaster = value;
                RaisePropertyChanged(() => CardMaster);
            }
        }

        [Column]
        public CardKindValue Kind
        {
            get { return kind; }
            set
            {
                kind = value;
                RaisePropertyChanged(() => Kind);
                RaisePropertyChanged(() => ShowCreditInfo);
            }
        }

        [Column]
        public string Alias
        {
            get { return alias; }
            set
            {
                alias = value;
                RaisePropertyChanged(() => Alias);
                RaisePropertyChanged(() => DisplayString);
            }
        }

        [Column]
        public double CreditLine
        {
            get { return creditLine; }
            set
            {
                creditLine = value;
                RaisePropertyChanged(() => CreditLine);
            }
        }

        [Column]
        public int MonthlyRepaymentDay
        {
            get { return monthlyRepaymentDay; }
            set
            {
                monthlyRepaymentDay = value;
                RaisePropertyChanged(() => MonthlyRepaymentDay);
                RaisePropertyChanged(() => NextRepaymentDate);
            }
        }

        public string DisplayString
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(Alias))
                {
                    return Alias;
                }
                return CardNumber;
            }
        }

        public DateTime NextRepaymentDate
        {
            get
            {
                int currentday = DateTime.Now.Day;
                if (currentday > MonthlyRepaymentDay)
                {
                    DateTime nextMonth = DateTime.Now.AddMonths(1);
                    return new DateTime(nextMonth.Year, nextMonth.Month, MonthlyRepaymentDay);
                }

                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, MonthlyRepaymentDay);
            }
        }

        public bool ShowCreditInfo
        {
            get
            {
                return Kind == CardKindValue.Credit;
            }
        }

        public bool RecentRepay { get; set; }
        #endregion
    }
}
